﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using AdoNet = System.Data;

namespace DataSet4Com
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ComDefaultInterface(typeof(IForeignKeyConstraint))]
    [Guid("3999EF0F-BC04-4975-BD0C-148B7675A757")]
    public class ForeignKeyConstraint : IForeignKeyConstraint, IConstraint
    {
        private AdoNet.ForeignKeyConstraint _adoNetForeignKeyConstraint;

        public ForeignKeyConstraint() : this(null)
        {}
                
        public ForeignKeyConstraint(AdoNet.ForeignKeyConstraint adoNetForeignKeyConstraint)
        {
            if (adoNetForeignKeyConstraint == null)
                throw new ArgumentNullException("adoNetForeignKeyConstraint");

            _adoNetForeignKeyConstraint = adoNetForeignKeyConstraint;
        }

        [ComVisible(false)]
        public AdoNet.ForeignKeyConstraint AdoNetForeignKeyConstraint
        {
            get { return _adoNetForeignKeyConstraint; }
        }

        public virtual AcceptRejectRule AcceptRejectRule
        {
            get { return (AcceptRejectRule)((int)_adoNetForeignKeyConstraint.AcceptRejectRule); }
            set { _adoNetForeignKeyConstraint.AcceptRejectRule = (AdoNet.AcceptRejectRule)((int)value); }
        }
        
        public virtual DataColumnList Columns 
        {
            get
            {
                DataColumn[] result = (from column in _adoNetForeignKeyConstraint.Columns
                                       select new DataColumn(column)).ToArray();

                return new DataColumnList(result);
            }
        }

        public virtual Rule DeleteRule
        {
            get { return (Rule)((int)_adoNetForeignKeyConstraint.DeleteRule); }
            set { _adoNetForeignKeyConstraint.DeleteRule = (AdoNet.Rule)((int)value); }
        }
        
        public virtual DataColumnList RelatedColumns 
        {
            get
            {
                DataColumn[] result = (from column in _adoNetForeignKeyConstraint.RelatedColumns
                                       select new DataColumn(column)).ToArray();

                return new DataColumnList(result);
            }
        }
        
        public virtual DataTable RelatedTable 
        {
            get
            {
                return new DataTable(_adoNetForeignKeyConstraint.RelatedTable);
            }
        }
        
        public DataTable Table
        {
            get
            {
                return new DataTable(_adoNetForeignKeyConstraint.Table);
            }
        }
        
        public virtual Rule UpdateRule
        {
            get { return (Rule)((int)_adoNetForeignKeyConstraint.UpdateRule); }
            set { _adoNetForeignKeyConstraint.UpdateRule = (AdoNet.Rule)((int)value); }
        }

        public bool Equals(IForeignKeyConstraint constraint)
        {
            return _adoNetForeignKeyConstraint.Equals(constraint);
        }

        public new int GetHashCode()
        {
            return _adoNetForeignKeyConstraint.GetHashCode();
        }

        public virtual string ConstraintName
        {
            get { return _adoNetForeignKeyConstraint.ConstraintName; }
            set { _adoNetForeignKeyConstraint.ConstraintName = value; }
        }

        [ComVisible(false)]
        public AdoNet.Constraint AdoNetConstraint
        {
            get { return (AdoNet.Constraint)_adoNetForeignKeyConstraint; }
        }
    }

    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [Guid("0B32F8B6-3FCD-4E49-98EE-C17314B20B14")]
    public interface IForeignKeyConstraint
    {
        AcceptRejectRule AcceptRejectRule { get; set; }

        DataColumnList Columns { get; }

        Rule DeleteRule { get; set; }

        DataColumnList RelatedColumns { get; }

        DataTable RelatedTable { get; }

        DataTable Table { get; }

        Rule UpdateRule { get; set; }

        bool Equals(IForeignKeyConstraint constraint);

        int GetHashCode();

        string ConstraintName { get; set; }
    }
}
